using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;

namespace CANBusTools.ViewModels
{
    public class IgnitionControlViewModel : ViewModelBase
    {
        private readonly object _busHelper;

        private string _selectedIgnition = string.Empty;
        public string SelectedIgnition
        {
            get => _selectedIgnition;
            set
            {
                if (_selectedIgnition != value)
                {
                    _selectedIgnition = value;
                    OnPropertyChanged(nameof(SelectedIgnition));
                }
            }
        }

        public ICommand SetIgnitionCommand { get; }

        public string[] IgnitionStates { get; } = { "OFF", "ACC", "RUN", "CRANK" };
        private readonly int[] IgnitionMap = { 1, 2, 3, 4 };

        public IgnitionControlViewModel(object busHelper)
        {
            _busHelper = busHelper;
            SelectedIgnition = IgnitionStates[0];
            SetIgnitionCommand = new RelayCommand(SetIgnition);
        }

        private void SetIgnition()
        {
            int index = Array.IndexOf(IgnitionStates, SelectedIgnition);
            if (index >= 0)
            {
                int value = IgnitionMap[index];
                // TODO: 替换为实际调用 _busHelper.IgnitionSwitch(value.ToString());
                Console.WriteLine($"Ignition set to {SelectedIgnition} ({value})");
            }
            else
            {
                Console.WriteLine("Invalid ignition state selected");
            }
        }
    }
}
